import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-28
 * Time: 0:20
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        // 1. 获取到用户提交的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String Confirm_password = req.getParameter("Confirm_password");
        System.out.println(password);
        System.out.println(Confirm_password);
        // 2. 判定用户名密码是否正确
        if (username.equals("")||!password.equals(Confirm_password)) {
// 注册失败
            resp.getWriter().write("注册失败");
            return;
        }
// 注册成功
        System.out.println("注册成功");
        // 设置 Session
        HttpSession session = req.getSession(true);
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        session.setAttribute("loginCount", "0");
        resp.sendRedirect("login.html");
    }
}
